#include "include/gudb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    printf("Testing New GuDB Features (ANALYZE, CHECKPOINT, VACUUM)\n");
    printf("========================================================\n");
    
    // 1. Open database
    printf("1. Opening database...\n");
    gudb* db = gudb_open("/tmp/test_new_features", GU_OPEN_CREATE | GU_OPEN_READWRITE, "admin", "admin");
    if (!db) {
        printf("❌ Failed to open database\n");
        return 1;
    }
    printf("✅ Database opened successfully\n");
    
    // 2. Create a test table with some data
    printf("\n2. Creating test data...\n");
    gudb_txn* txn = gudb_begin(db, GU_TXN_RC, "test_db");
    if (!txn) {
        printf("❌ Failed to begin transaction\n");
        gudb_close(db);
        return 1;
    }
    
    result_t* result = NULL;
    
    // Create database and table
    if (gudb_exec(txn, "CREATE DATABASE test_db", NULL, &result) != GU_OK) {
        printf("❌ Failed to create database\n");
        gudb_rollback(txn);
        gudb_close(db);
        return 1;
    }
    if (result) gudb_free_result(result);
    
    if (gudb_exec(txn, "USE test_db", NULL, &result) != GU_OK) {
        printf("❌ Failed to use database\n");
        gudb_rollback(txn);
        gudb_close(db);
        return 1;
    }
    if (result) gudb_free_result(result);
    
    if (gudb_exec(txn, "CREATE TABLE test_table (id INT PRIMARY KEY, name VARCHAR(50))", NULL, &result) != GU_OK) {
        printf("❌ Failed to create table\n");
        gudb_rollback(txn);
        gudb_close(db);
        return 1;
    }
    if (result) gudb_free_result(result);
    
    // Insert some test data
    for (int i = 1; i <= 5; i++) {
        char insert_sql[256];
        snprintf(insert_sql, sizeof(insert_sql), "INSERT INTO test_table VALUES (%d, 'Test%d')", i, i);
        if (gudb_exec(txn, insert_sql, NULL, &result) != GU_OK) {
            printf("❌ Failed to insert data row %d\n", i);
        }
        if (result) gudb_free_result(result);
    }
    
    gudb_commit(txn);
    printf("✅ Test data created successfully\n");
    
    // 3. Test ANALYZE TABLE functionality
    printf("\n3. Testing ANALYZE TABLE functionality...\n");
    int analyze_result = gudb_analyze_table(db, "test_table");
    if (analyze_result == GU_OK) {
        printf("✅ ANALYZE TABLE completed successfully\n");
    } else {
        printf("❌ ANALYZE TABLE failed with error code: %d\n", analyze_result);
    }
    
    // 4. Test CHECKPOINT functionality
    printf("\n4. Testing CHECKPOINT functionality...\n");
    int checkpoint_result = gudb_checkpoint(db);
    if (checkpoint_result == GU_OK) {
        printf("✅ CHECKPOINT completed successfully\n");
    } else {
        printf("❌ CHECKPOINT failed with error code: %d\n", checkpoint_result);
    }
    
    // 5. Test VACUUM functionality
    printf("\n5. Testing VACUUM functionality...\n");
    int vacuum_result = gudb_vacuum(db);
    if (vacuum_result == GU_OK) {
        printf("✅ VACUUM completed successfully\n");
    } else {
        printf("❌ VACUUM failed with error code: %d\n", vacuum_result);
    }
    
    // 6. Clean up
    printf("\n6. Cleaning up...\n");
    gudb_close(db);
    printf("✅ Database closed successfully\n");
    
    printf("\n========================================================\n");
    printf("New Features Test Complete!\n");
    printf("All implemented TODO features have been tested.\n");
    
    return 0;
}